<?php
/**
 * Created by PhpStorm.
 * User: fenfen
 * Date: 2017/1/3
 * Time: 17:51
 */

namespace bc\model;

use bc\ar\MActiveRecord;
class VdreferenceModel extends MActiveRecord
{
    private static $grade=array(
        1=>'g',
        2=>'d',
        3=>'z'
    );

    private static $chtotag=array(
        '备孕'=>'',
        '孕早期'=>'yz-',
        '孕中晚'=>'yw-',
        '哺乳期'=>'yw-',
    );

    private static $tag=array(
        1=>'VD-',
        2=>'GAI-'
    );

    public static function tableName()
    {
        return '{{%vd_reference}}';
    }

    public function getReference($grade_p,$chtotag_p,$tag_p)
    {
//        var_dump($grade_p.$chtotag_p.$tag_p) ;
        if(isset(self::$chtotag[$chtotag_p])){
            $where = array('type_name'=>self::$tag[$tag_p].self::$chtotag[$chtotag_p].self::$grade[$grade_p]);
        }else{
            $where = array('type_name'=>self::$tag[$tag_p].self::$grade[$grade_p]);
        }

//        var_dump($where);
//        $where = array('type_name'=>'VD-'.'d');
        $ar = $this->find ()
            ->select ("`age_0` as '0',`age_0.5` as `0.5`,`age_1` as '1',`age_2` as '2',`age_3` as '3',`age_4` as '4',
                    `age_5` as '5',`age_6` as '6',`age_7` as '7',`age_8` as '8',`age_9` as '9',`age_10` as '10',`age_11` as '11',
                    `age_12` as '12',`age_13` as '13',`age_14` as '14',`age_15` as '15',`age_16` as '16',`age_17` as '17',`age_18` as '18',`age_19` as '19',`age_20` as '20',`age_21` as '21',`age_22` as '22',`age_23` as '23',`age_24` as '24',`age_25` as '25',`age_26` as '26',`age_27` as '27',`age_28` as '28',
                    `age_29` as '29',`age_30` as '30',`age_31` as '31',`age_32` as '32',`age_33` as '33',`age_34` as '34',`age_35` as '35',`age_36` as '36',
                    `age_37` as '37',`age_38` as '38',`age_39` as '39',`age_40` as '40',`age_41` as '41',`age_42` as '42',`age_43` as '43',`age_44` as '44',`age_45` as '45',
                    `age_46` as '46',`age_47` as '47',`age_48` as '48',`age_49` as '49',`age_50` as '50',`age_51` as '51',`age_52` as '52',`age_53` as '53',`age_54` as '54',`age_55` as '55',`age_56` as '56',`age_57` as '57',
                    `age_58` as '58',`age_59` as '59',`age_60` as '60',`age_61` as '61',`age_62` as '62',`age_63` as '63',`age_64` as '64',`age_65` as '65',`age_mt` as '66'" )
            ->where ( $where );
//        echo $ar->createCommand()->getRawSql();
        return $ar->asArray ()->one();
    }

    public function getRefData($age,$dataArr)
    {
        is_array($dataArr) or die();
        if($age < 0)
            die();

        foreach($dataArr as $key => $value)
        {
            if($age <= $key)
            {
                return $value;
            }
        }

        return $dataArr['66'];
    }
}